<template>
  <div class="playlist-container">
    <div class="top-wrap">
      <div class="img-wrap">
        <img :src="playlist.coverImgUrl" alt="" />
      </div>
      <div class="info-wrap">
        <p class="title">{{playlist.name}}</p>
        <div class="author-wrap">
          <img class="avatar" v-if="playlist.creator !== undefined" :src="playlist.creator.avatarUrl" alt="" />
          <span class="name" v-if="playlist.creator !== undefined">{{playlist.creator.nickname}}</span>
          <span class="time">{{playlist.createTime | transDate}} 创建</span>
        </div>
        <div class="play-wrap">
          <span class="iconfont icon-circle-play"></span>
          <span class="text">播放全部</span>
        </div>
        <div class="tag-wrap">
          <span class="title">标签:</span>
          <ul>
            <li v-for="(item, index) in playlist.tags" :key="index">{{item}}</li>
            <!-- <li>怀旧</li> -->
            <!-- <li>感动</li> -->
          </ul>
        </div>
        <div class="desc-wrap">
          <span class="title">简介:</span>
          <span v-if="playlist.description" class="desc">{{playlist.description}}</span>
          <span v-else class="desc">下次网购时，也记得把默认收货地址换了</span>
        </div>
      </div>
    </div>
    <el-tabs v-model="activeIndex">
      <el-tab-pane label="歌曲列表" name="1">
        <table class="el-table playlit-table">
          <thead>
            <th></th>
            <th></th>
            <th>音乐标题</th>
            <th>歌手</th>
            <th>专辑</th>
            <th>时长</th>
          </thead>
          <tbody>
            <tr class="el-table__row"
            v-for="(item, index) in playlist.tracks" :key="index"
            >
              <td>{{index + 1}}</td>
              <td>
                <div class="img-wrap"  @click="playMusic(item.id)">
                  <img :src="item.al.picUrl" alt="" />
                  <span class="iconfont icon-play"></span>
                </div>
              </td>
              <td>
                <div class="song-wrap">
                  <div class="name-wrap">
                    <span>{{ item.name }}</span>
                    <span v-if="item.mv !== 0" class="iconfont icon-mv" @click="$router.push(`/mv?id=${item.mv}`)"></span>
                  </div>
                  <span v-if="item.alia.length !== 0">{{item.alia[0]}}</span>
                </div>
              </td>
              <td>{{ item.ar[0].name }}</td>
              <td>{{ item.al.name }}</td>
              <td>{{ item.dt | playTimeFormat}}</td>
            </tr>
          </tbody>
        </table>
      </el-tab-pane>
      <el-tab-pane :label="`评论(${total})`" name="2">
        <!-- 精彩评论 -->
        <div class="comment-wrap">
          <p class="title">精彩评论<span class="number">({{hotCount}})</span></p>
          <div class="comments-wrap">
            <div class="item"
            v-for="(item, index) in hotComments"
            :key="index"
            >
              <div class="icon-wrap">
                <img :src="item.user.avatarUrl" alt="" />
              </div>
              <div class="content-wrap">
                <div class="content">
                  <span class="name">{{item.user.nickname}}：</span>
                  <span class="comment">{{item.content}}</span>
                </div>
                <div class="re-content" v-if="item.beReplied.length != 0">
                  <span class="name">{{item.beReplied[0].user.nickname}}：</span>
                  <span class="comment">{{item.beReplied[0].content}}</span>
                </div>
                <div class="date">{{item.time | transDate}}</div>
              </div>
            </div>
          </div>
        </div>
        <!-- 最新评论 -->
        <div class="comment-wrap">
          <p class="title">最新评论<span class="number">({{total}})</span></p>
          <div class="comments-wrap">
            <div class="item" 
            v-for="(item, index) in comments"
            :key="index"
            >
              <div class="icon-wrap">
                <img :src="item.user.avatarUrl" alt="" />
              </div>
              <div class="content-wrap">
                <div class="content">
                  <span class="name">{{item.user.nickname}}：</span>
                  <span class="comment">{{item.content}}</span>
                </div>
                <div class="re-content" v-if="item.beReplied.length != 0">
                  <span class="name">{{item.beReplied[0].user.nickname}}：</span>
                  <span class="comment">{{item.beReplied[0].content}}</span>
                </div>
                <div class="date">{{item.time | transDate}}</div>
              </div>
            </div>
          </div>
        </div>
        <!-- 分页器 -->
        <el-pagination
          @current-change="handleCurrentChange"
          background
          layout="prev, pager, next"
          :total="total"
          :current-page="page"
          :page-size="5"
        >
        </el-pagination>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import { PlayDetail, Hot, NewHot, playMusic } from '../network/request'
export default {
  name: 'playlist',
  data() {
    return {
      activeIndex: '1',
      // 总条数
      total: 0,
      // 页码
      page: 1,
      limit: 10,
      offset: 1,

      playlist: {},
      hotComments: [],
      hotCount:0,
      comments: [],
      // commCount:0
    };
  },
  async created() {
    this.detailList()

    //热
    this.hot(this.$route.query.id)
    this.newhot(this.$route.query.id)
  },
  methods: {
    async detailList() {
      const res = await PlayDetail(this.$route.query.id)
      console.log(res)
      this.playlist = res.playlist;
    },
    async hot(id) {
      const res = await Hot(id, 2, this.limit)
      this.hotComments = res.hotComments;
      this.hotCount = res.total;
    },
    //新评论
    async newhot(id) {
      const res = await NewHot(id, this.limit, (this.page - 1) * this.limit)
      this.comments = res.comments;
      this.total = res.total;
    },
    handleCurrentChange(val) {
      this.page = val;
      this.newhot(this.$route.query.id)
    },
    async playMusic(id) {
      const res = await playMusic(id)
      if (!res.data[0].url) {
        return this.$message.error('该资源为VIP专享，暂不支持播放 ！')
      }
      // 设置给父组件的播放地址
      this.$parent.musicUrl = res.data[0].url
    }
  }
};
</script>

<style >

</style>
